package com.qijian.maindata.domain;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qijian.common.annotation.Excel;
import com.qijian.common.config.jackson.serialize.LongListToStringSerialize;
import com.qijian.common.core.domain.BaseEntity;
import com.qijian.common.exception.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 工装台账对象 main_frock_ledger
 *
 * @author qijian
 * @date 2024-02-04
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString
@TableName("main_frock_ledger")
public class FrockLedger extends BaseEntity {
    private static final long serialVersionUID = 1L;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "id")
    private Long id;

    @Excel(name = "使用厂区")
    @ApiModelProperty(value = "使用厂区")
    private String factoryArea;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "使用厂区id")
    private Long factoryAreaId;


    @Excel(name = "区域")
    @ApiModelProperty(value = "区域")
    private String location;

    @Excel(name = "工装名称")
    @ApiModelProperty(value = "工装名称")
    private String name;

    @Excel(name = "开工令")
    @ApiModelProperty(value = "开工令")
    private String projectNo;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "开工令id")
    private Long projectId;
    @Excel(name = "数量")
    @ApiModelProperty(value = "数量")
    private BigDecimal quantity;
    @Excel(name = "重量")
    @ApiModelProperty(value = "重量")
    private BigDecimal weight;


    @Excel(name = "使用部门")
    @ApiModelProperty(value = "使用部门")
    private String deptName;



    @ApiModelProperty(value = "使用部门id")
    private String deptId;


    @Excel(name = "使用时间")
    @ApiModelProperty(value = "使用时间")
    private String year;


    @ApiModelProperty(value = "备注")
    private String remark;

    @TableField(exist = false)
    @JsonSerialize(using = LongListToStringSerialize.class)
    private List<Long> areaIds;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "归属区域id")
    private Long belongAreaId;

    @ApiModelProperty(value = "归属区域")
    private String belongArea;

    @Excel(name = "状态" , dictType = "equipment_ledger_status" )
    @ApiModelProperty(value = "状态")
    private String status;

    /**
     * 填充归属区域
     *
     * @param first 区域明细
     */
    public void fillBelong(LedgerAreaDetail first) {
        setBelongArea(first.getArea());
        setBelongAreaId(first.getAreaId());
    }

    /**
     * 校验部门
     *
     * @param dictMap
     */
    public void checkDept(Map<String, String> dictMap) {
        String deptId = dictMap.get(this.getDeptName());
        if (StrUtil.isEmpty(deptId)) {
            throw new ServiceException(StrUtil.format("使用部门{}不合法", this.getDeptName()));
        }
        this.setDeptId(deptId);
    }

    /**
     * 校验使用厂区
     *
     * @param classifyList
     */
    public void checkFactoryArea(List<CommonClassify> classifyList) {
        Optional<CommonClassify> factoryArea = classifyList.stream().filter(item -> Objects.equals(item.getName(), this.getFactoryArea())).findFirst();
        if (!factoryArea.isPresent()) {
            throw new ServiceException(StrUtil.format("{} 厂区未维护", this.getFactoryArea()));
        }
        Long factoryAreaId = factoryArea.get().getId();
        this.setFactoryAreaId(factoryAreaId);
    }

    public void checkProjectNo(List<Project> projects){
        if (StrUtil.isNotEmpty(this.getProjectNo())) {
            Optional<Project> project = projects.stream().filter(item -> Objects.equals(item.getProjectMachineNo(), this.getProjectNo())).findFirst();
            if (!project.isPresent()) {
                throw new ServiceException(StrUtil.format("{} 开工令不存在", this.getProjectNo()));
            }
            this.setProjectId(project.get().getProjectId());
        }
    }

}
